# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=babel
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=2.2.0
pkgrel=1
pkgdesc="A collection of tools for internationalizing Python applications"
url="http://babel.pocoo.org/"
license=("BSD")
arch=('any')
makedepends=("${MINGW_PACKAGE_PREFIX}-python2-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python2-pytz"
             "${MINGW_PACKAGE_PREFIX}-python3-pytz")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/mitsuhiko/babel/archive/${pkgver}.tar.gz")
sha256sums=('2a519819e607360160e135f58f911b1a44c92910a3c7fc771618fb494f391e6c')

prepare() {
  cd ${srcdir}
  cp -r ${_realname}-${pkgver} build-python2
  cp -r ${_realname}-${pkgver} build-python3
}

package_python3-babel() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3-pytz")

  cd ${srcdir}/build-python3
  ${MINGW_PREFIX}/bin/python3 setup.py import_cldr
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} --root="${pkgdir}"

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/LICENSE"

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|${PREFIX_WIN}/bin/|/usr/bin/env |g" -i ${_f}
  done
}

package_python2-babel() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2-pytz")

  cd ${srcdir}/build-python2
  ${MINGW_PREFIX}/bin/python2 setup.py import_cldr
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} --root="${pkgdir}"

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/LICENSE"

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|${PREFIX_WIN}/bin/|/usr/bin/env |g" -i ${_f}
  done

  for f in pybabel; do
    mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,2}.exe
    if [ -f "${pkgdir}${MINGW_PREFIX}"/bin/${f}.exe.manifest ]; then
      mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,2}.exe.manifest
      sed -e "s|${f}|${f}2|g" -i "${pkgdir}${MINGW_PREFIX}"/bin/${f}2.exe.manifest
    fi
    mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,2}-script.py
  done
}

package_mingw-w64-i686-python2-babel() {
  package_python2-babel
}

package_mingw-w64-i686-python3-babel() {
  package_python3-babel
}

package_mingw-w64-x86_64-python2-babel() {
  package_python2-babel
}

package_mingw-w64-x86_64-python3-babel() {
  package_python3-babel
}
